package supertrunfo;

import daos.LoginDAOS;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class ExcluirConta extends javax.swing.JInternalFrame {

    private static ResourceBundle myResources;
    private static final Logger log = Logger.getLogger(ExcluirConta.class.getName());
    /** Creates new form ExcluirConta */
    public ExcluirConta(String titulo, boolean resizable, boolean closeable, boolean maximizable, boolean iconifiable) {
        super(titulo,resizable,closeable,maximizable,iconifiable);
        try {

            Handler console = new ConsoleHandler();
            Handler file = new FileHandler("C:\\SuperTropa.log");

            /*Define que na consola apenas aparece log com nível superior ou
              a warning e no ficheiro deve aparecer o log de qualquer nível
             */

            console.setLevel(Level.INFO);
            file.setLevel(Level.ALL);
            //Define o formato de output do ficheiro como XML
            file.setFormatter(new SimpleFormatter());
            //Adiciona os handlers para ficheiro e console
            log.addHandler(file);
            log.addHandler(console);
            //Ignora os Handlers definidos no Logger Global
            log.setUseParentHandlers(false);
        }

        catch(IOException io){
            log.warning("O ficheiro hellologgin.xml não pode ser criado");
        }
        if (Login.LinguaAtual.equals("Português")) {
           myResources =  ResourceBundle.getBundle("Message_pt_BR",Locale.getDefault());
        }
        else {
           myResources =  ResourceBundle.getBundle("Message_en_US",Locale.ENGLISH);
        }
        log.info("Tela de escluir conta iniciada.");
        initComponents();
        getRootPane().setDefaultButton(bConfirmar);
        bConfirmar.setToolTipText(myResources.getString("excluir.infConfirmar"));
        tLogin.setToolTipText(myResources.getString("excluir.infLogin"));
        tSenha.setToolTipText(myResources.getString("excluir.infSenha"));
        setVisible(true);
        setLocation(200,50);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        bConfirmar = new javax.swing.JButton();
        tSenha = new javax.swing.JTextField();
        tLogin = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        jLayeredPane1.setBackground(new java.awt.Color(255, 255, 255));
        jLayeredPane1.setOpaque(true);

        jLabel2.setFont(new java.awt.Font("Showcard Gothic", 0, 11));
        jLabel2.setText(myResources.getString("excluir.login"));
        jLabel2.setBounds(60, 80, 50, 15);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("Showcard Gothic", 0, 11));
        jLabel1.setText(myResources.getString("excluir.senha"));
        jLabel1.setBounds(56, 110, 40, 15);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setFont(new java.awt.Font("A Damn Mess", 0, 30));
        jLabel3.setText(myResources.getString("titulo.excluir"));
        jLabel3.setBounds(30, 20, 250, 34);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bConfirmar.setFont(new java.awt.Font("Showcard Gothic", 0, 11));
        bConfirmar.setText(myResources.getString("excluir.confirmar"));
        bConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bConfirmarActionPerformed(evt);
            }
        });
        bConfirmar.setBounds(100, 150, 116, 23);
        jLayeredPane1.add(bConfirmar, javax.swing.JLayeredPane.DEFAULT_LAYER);
        tSenha.setBounds(110, 110, 138, 20);
        jLayeredPane1.add(tSenha, javax.swing.JLayeredPane.DEFAULT_LAYER);
        tLogin.setBounds(110, 80, 138, 20);
        jLayeredPane1.add(tLogin, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setBackground(new java.awt.Color(255, 255, 255));
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/supertrunfo/stcI2.jpg"))); // NOI18N
        jLabel4.setBounds(0, 20, 450, 310);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 198, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Quando clicar no Confirmar, se o login e a senha conferir com Banco de Dados, então exclui a conta do usuário.
     * @param evt
     */
    private void bConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bConfirmarActionPerformed
        int resp=JOptionPane.showConfirmDialog(null, myResources.getString("excluir.excluir"));
        if(resp==0)
        {
            String loging, senha;
            loging = tLogin.getText();
            senha = tSenha.getText();
            if(LoginDAOS.excluirConta(loging,senha)==1){
                System.out.println(myResources.getString("excluir.ok"));
                this.hide();
                log.info("Conta de usuario escluida com sucesso.");
                new Login().setVisible(true);
            }
            else{
                log.warning("Falha ao excluir a conta.");
                System.out.println(myResources.getString("excluir.erro"));
            }
        }

    }//GEN-LAST:event_bConfirmarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bConfirmar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JTextField tLogin;
    private javax.swing.JTextField tSenha;
    // End of variables declaration//GEN-END:variables

}
